<?php

class c_router {

    private $path;
    private $default;
    
    public  $controller;
    public  $action;

    function __construct($default) {
        $this->path = MVC_BASE_PATH;
        $this->default = explode('/', $default);
    }

    public function loader() {
        global $modx;
        
        $route = (empty($_GET['action'])) ? $this->default : $_GET['action'];
        
        $parts = array();
        
        if (!empty($_GET['action'])){
            $parts = explode("/",$_GET['action']);
        }

        //Переписываем контроллер и другие параметы
        if (!empty($this->default)){
            $parts = $this->default+$parts; //http://www.php.net/manual/en/function.array-merge.php
        }

        $controller = "c_".$parts[0]."_controller";
        if (!empty($parts[1])) {
            $action = str_replace(".html","",$parts[1]);
        }else{
            $action = "index";
        }

        $app = new $controller();

        if (!method_exists($app,$action)){
            $modx->sendErrorPage();
        }

        return $app->$action($parts);
    }

}

?>

